#include "StdAfx.h"
#include "IpcChannel.h"

const UINT IPC_BUFFER_SIZE = 4*1024*1024;	// 4M

IpcChannel::IpcChannel() : _mmaphandle(NULL)
{
}

IpcChannel::~IpcChannel()
{
	destroy();
}

bool IpcChannel::createMaster( PCTSTR name )
{
	_mmaphandle = CreateFileMapping(
		INVALID_HANDLE_VALUE,    // use paging file
		NULL,                    // default security 
		PAGE_READWRITE,          // read/write access
		0,                       // max. object size 
		IPC_BUFFER_SIZE,                // buffer size  
		name);                 // name of mapping object
	bool result = _mmaphandle != NULL && _mmaphandle != INVALID_HANDLE_VALUE; 

	if(result)
		_mutex = CreateMutex(NULL, false, _T("mmmutex"));
	else
		_mmaphandle = NULL;
	
	return result;
}

bool IpcChannel::createSlave( PCTSTR name )
{
	_mmaphandle = OpenFileMapping(
		FILE_MAP_ALL_ACCESS,   // read/write access
		FALSE,                 // do not inherit the name
		name);               // name of mapping object 

	_mutex = OpenMutex(
		EVENT_ALL_ACCESS,
		false,
		_T("mmmutex")
		);

	return _mmaphandle != NULL;
}

void IpcChannel::destroy()
{
	CloseHandle(_mutex);
	if(_mmaphandle)
	{
		CloseHandle(_mmaphandle);
		_mmaphandle = NULL;
	}
}

bool IpcChannel::write( void* buf, UINT bufsize )
{
	return false;
}

bool IpcChannel::read( UINT& blockcount, IpcBlock block[] )
{
	return false;
}

